// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'                             
import axios from 'axios'
import echarts from 'echarts'
import iView from 'iview'
import locale from 'iview/dist/locale/zh-CN'
import { Notice } from 'iview'
import 'iview/dist/styles/iview.css'
import '../static/css/gobal.css'
import 'babel-polyfill'

Vue.use(iView, { locale });

Vue.config.productionTip = false

axios.defaults.baseURL = process.env.ApiUrl;

Vue.prototype.$Notice = Notice
//axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
// axios.defaults.headers.post['Access-Control-Allow-Origin'] = '*/*';

// 添加请求拦截器
axios.interceptors.request.use(
  config => {
    var qs = require('qs');
    config.data = qs.stringify(config.data);
    let token = localStorage.getItem("token")
    if (token) {
      config.headers['UFDM-AuthToken'] = token
    }
    return config;
  }, error => {
    return Promise.reject(error);
  });
  
// 添加响应拦截器
axios.interceptors.response.use(
  response => {
    if (response.data.retHead.code == '0101005' 
        || response.data.retHead.code == '0101006'  
        || response.data.retHead.code == '0101007'  
        || response.data.retHead.code == '0101008'  
        || response.data.retHead.code == '0101009') {
          //token失效
      //alert(response.data.msg);
      window.location.href = '/#/login'//需求方要求一旦出错立即跳转登录，所以采取这种侵入式的手段。
    } else {
      return response.data
    }
  }, error => {
    // let response = error.response;
    // if (!response) {
    //   ElementUI.Message({message: error.message, type: 'error'})
    // } else if (response.status == 401) {
    //   localStorage.removeItem("token")
    //   return response.data;
    // } else if (response.status == 403) {
    //   ElementUI.Message({message: response.data.message, type: 'warning'})
    // } else if (response.status >= 500) {
    //   ElementUI.Message({message: response.data.message, type: 'error'})
    // }
    return Promise.reject(error);
  });
router.beforeEach((to, from, next) => {
  if (to.path == '/login') {
    localStorage.removeItem('token');
    localStorage.removeItem("open")
    localStorage.removeItem("active");
  }

  let token = localStorage.getItem('token');
  if (!token && to.path != '/login') {
    //next()
    localStorage.removeItem("open")
    localStorage.removeItem("active");
    next({ path: '/login' })
  } else {
    next()
  }
})

Vue.prototype.$echarts = echarts
Vue.prototype.$http = axios;

Vue.prototype.padDate =function(va){
    va=va<10?'0'+va:va;
    return va;
}

Vue.prototype.formatDate = function(date,format){
    if(date==null || date==''){
      return;
    }else{
      var date1 = new Date(date);
      if(format=="yyyy-MM-dd" || format=="yyyy-mm-dd"){
          return  date1.getFullYear() + '-' + this.padDate((date1.getMonth() + 1)) + '-' +this.padDate(date1.getDate());
      }
      if(format=="yyyymmdd" || format=="yyyyMMdd"){
          return date1.getFullYear()  + this.padDate((date1.getMonth() + 1)) + this.padDate(date1.getDate());
      }
      if(format=="yyyymm" || format=="yyyyMM"){
        return date1.getFullYear()  + this.padDate((date1.getMonth() + 1));
       }
      return date1;
    }

}


/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})

